package main

import (
	"gitee.com/casbin-demon-gin/model"
	"github.com/gin-gonic/gin"
)

// https://github.com/me-kuer/crm-server

func main() {
	g:=gin.Default()
	g.Use(CasbinMiddleware())
	g.POST("/member/login",Login)
	g.POST("/member/Logout",Logout)
	g.GET("/member/list",List)
	g.GET("/member/detail",Detail)
	g.POST("/member/mod",Mod)
	g.GET("/member/del",Del)

	if err:=g.Run(":18080");err!=nil{
		panic(err)
	}
}

func CasbinMiddleware() gin.HandlerFunc {
	return func(context *gin.Context) {
		sub := context.Request.Header.Get("userid")
		obj := context.Request.URL.RequestURI()
		act := context.Request.Method
		if ok,_:=model.Enforcer.Enforce(sub,obj,act);ok{
			context.Next()
		}else {
			context.JSON(200,"权限不足")
			context.Abort()
			return
		}
	}
}

// 登录
func Login(ctx *gin.Context)  {
	userName :=ctx.Query("username")
	password :=ctx.Query("password")

	if len(userName) == 0 || len(password) == 0 {
		ctx.JSON(200,"用户密码输入错误")
		return
	}
	var member model.Member
	if err:=model.DB.Select("id,status").Where("username=? AND password=?",userName,password).First(&member).Error;err!=nil{
		ctx.JSON(200,err)
		return
	}
	if member.Status !=1{
		ctx.JSON(200,"禁止登录")
		return
	}
	ctx.JSON(200,member)
}

// 退出登录
func Logout(ctx *gin.Context)  {

}

/*
	select
		c.id, c.name, c.stage, c.addr, c.tag, c.company, c.tel, c.create_time, u.name as user_name
	from
		customer as c
	left join
		users as u
	on
		c.user_id = u.id
	where
		c.status = 1
	order by
		c.id desc
	limit 1,10
 */

//用户列表
func List(ctx *gin.Context)  {
	//model.DB.Table("member").Select("member.id,member.name,member.tel,member.username,role.name").Joins("left join role on member.id=role.id")
}

// 用户详情
func Detail(ctx *gin.Context)  {

}

//用户修改
func Mod(ctx *gin.Context)  {

}

//删除用户
func Del(ctx *gin.Context)  {

}

// 给角色添加权限
func Add()  {
	model.Enforcer.AddPolicy("","","")
}

// 删除角色权限
func Remove()  {
	model.Enforcer.RemovePolicy()
}


/*
var DB *gorm.DB

func Casbin() *casbin.Enforcer {
	a, err := gormadapter.NewAdapterByDB(DB)
	if err != nil {
		panic(err)
	}
	m, err := cmodel.NewModelFromString(`
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`)
	if err!=nil{
		panic(err)
	}
	// 会自动创建casbin-rule
	e, err := casbin.NewEnforcer(m, a)
	if err != nil {
		panic(err)
	}
	_ = e.LoadPolicy()
	return e
}

func AddCasbin(cm model.CasbinModel) bool {
	e := Casbin()
	ok, err := e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
	if ok && err == nil {
		return true
	}
	return false
}

func ClearCasbin(v int, p ...string) bool {
	e := Casbin()
	ok, err := e.RemoveFilteredPolicy(v, p...)
	if ok && err == nil {
		return true
	}
	return false
}

type CasbinInfo struct {
	Path   string `json:"path"`
	Method string `json:"method"`
}

// [eg:更改角色权限]
func UpdateCasbin(authorityId string, casbinInfo []CasbinInfo) error {
	ClearCasbin(0, authorityId)
	for _, i2 := range casbinInfo {
		cm := model.CasbinModel{
			ID:          0,
			Ptype:       "p",
			AuthorityId: authorityId,
			Path:        i2.Path,
			Method:      i2.Method,
		}
		addflag := AddCasbin(cm)
		if !addflag {
			return errors.New("add casbin error")
		}
	}
	return nil
}

// [eg: 获取权限列表]
func GetPolicyPathByAuthorityId(authorityId string) (array []CasbinInfo) {
	e := Casbin()
	list := e.GetFilteredPolicy(0, authorityId)
	for _, i2 := range list {
		array = append(array, CasbinInfo{
			Path:   i2[1],
			Method: i2[2],
		})
	}
	return
}

 */